<template>
  <div class="AdminNewsEdit">
    <h1>AdminNews Edit</h1>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item label="详情" prop="detail">
        <el-input v-model="ruleForm.detail" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" required>
        <el-col :span="12">
          <el-form-item prop="create_at">
            <el-date-picker
              v-model="ruleForm.create_at"
              type="date"
              placeholder="Pick a date"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>

      <el-form-item label="发布者" prop="region">
        <el-select v-model="ruleForm.author" placeholder="请选择发布者">
          <el-option label="新华社" value="新华社"></el-option>
          <el-option label="人民日报" value="人民日报"></el-option>
          <el-option label="央视网" value="央视网"></el-option>
          <el-option label="张三频道" value="张三频道"></el-option>
          <el-option label="凤凰新闻" value="凤凰新闻"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleFormRef')"
          >保存</el-button
        >
        <el-button @click="resetForm">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
/*
 * ref 值类型
 * reactive 对象
 * */

import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";

export default {
  name: "AdminNewsEdit",
  setup() {
    let rules = ref({
      title: [
        { required: true, message: "请输入标题", trigger: "blur" },
        { min: 5, max: 30, message: "长度在 5 ~ 30 之间", trigger: "change" },
      ],
      create_at: [
        {
          type: "date",
          required: true,
          message: "Please pick a date",
          trigger: "change",
        },
      ],
      desc: [{ required: true, message: "请输入描述", trigger: "blur" }],
      detail: [{ required: true, message: "请输入详情", trigger: "blur" }],
    });
    const route = useRoute();
    const router = useRouter();
    const id = route.params.id;
    const store = useStore();
    let ruleForm = ref({
      id: "",
      title: "",
      desc: "",
      detail: "",
      create_at: "",
      author: "",
    });

    // 回填数据
    const getNews = async () => {
      const res = await store.dispatch("news/getNewsAction", id);
      ruleForm.value = res.data;
    };
    getNews();

    const ruleFormRef = ref();
    const submitForm = () => {
      ruleFormRef.value.validate(async (valid) => {
        if (valid) {
          await store.dispatch("news/updateNewsAction", {
            id,
            data: ruleForm.value,
          });
          router.push({
            name: "AdminNews",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    };

    const resetForm = () => {
      ruleFormRef.value.resetFields();
    };

    return {
      resetForm,
      submitForm,
      ruleFormRef,
      ruleForm,
      rules,
    };
  },
};
</script>

<style scoped></style>
